/**  
 * @Title: CmpExecSqlService.java
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-05-06 14:23:02 
 */  
package com.fsxgt.datagrid.component.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.fsxgt.datagrid.component.bo.CmpExecSqlBO;
import com.fsxgt.datagrid.component.dao.CmpExecSqlMapper;
import com.fsxgt.datagrid.ds.component.MybatisComponent;
import com.fsxgt.datagrid.ds.component.QueryComponent;
import com.fsxgt.datagrid.ds.handler.DbHandler;
import com.fsxgt.datagrid.ds.utils.ParamUtil;
import com.fsxgt.datagrid.ds.utils.ParamUtil.ParamBO;
import com.fsxgt.datagrid.flow.dao.FlowInfoMapper;
import com.fsxgt.datagrid.flow.service.IComponentService;

import lombok.extern.slf4j.Slf4j;

/**  
 * @ClassName: CmpExecSqlService
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-05-06 14:23:02 
*/
@Slf4j
@Service
public class CmpExecSqlService implements IComponentService{
	
	@Autowired
	private FlowInfoMapper flowInfoMapper;
	
	@Autowired
	private CmpExecSqlMapper cmpExecSqlMapper;
	
	@Autowired
	private MybatisComponent mybatisComponent;
	@Autowired
	private QueryComponent queryComponent;
	
	

	
	@Transactional(rollbackFor = Exception.class)
	public void saveCmpExecSql(CmpExecSqlBO CmpExecSqlBO) {
		CmpExecSqlBO bo = cmpExecSqlMapper.get(CmpExecSqlBO.getFlowInfoId(),CmpExecSqlBO.getComponentSeq());
		if(bo==null) {
			cmpExecSqlMapper.insert(CmpExecSqlBO);
		}else {
			cmpExecSqlMapper.update(CmpExecSqlBO);
		}
		
	
		
		flowInfoMapper.updateFlowComponentName(CmpExecSqlBO.getName(), CmpExecSqlBO.getFlowInfoId(), CmpExecSqlBO.getComponentSeq());

		
	}
	@Transactional(rollbackFor = Exception.class)
	public void deleteComponent(CmpExecSqlBO CmpExecSqlBO) {
		deleteComponent(CmpExecSqlBO.getFlowInfoId(),CmpExecSqlBO.getComponentSeq());
	}
	/**  
	 * @Title: deleteComponent
	 * @Description: TODO(描述)
	 * @param flowInfoId
	 * @param seq 
	 * @see com.fsxgt.datagrid.core.service.impl.IComponentService#deleteComponent(java.lang.String, java.lang.String) 
	 * @author liuyi
	 * @date 2023-05-08 09:08:12 
	 */ 
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteComponent(String flowInfoId, String seq) {
		// TODO Auto-generated method stub
		cmpExecSqlMapper.delete(flowInfoId,seq);

	}
	/**  
	 * @Title: getComponentType
	 * @Description: TODO(描述)
	 * @return 
	 * @see com.fsxgt.datagrid.core.service.impl.IComponentService#getComponentType() 
	 * @author liuyi
	 * @date 2023-05-08 09:08:12 
	 */ 
	@Override
	public String getComponentType() {
		// TODO Auto-generated method stub
		return "Exec Sql";
	}
	/**  
	 * @Title: getSyncOffline
	 * @Description: TODO(描述)
	 * @param bo
	 * @author liuyi
	 * @date 2023-05-08 09:51:14 
	 */  
	public CmpExecSqlBO getCmpExecSql(CmpExecSqlBO CmpExecSqlBO) {
		// TODO Auto-generated method stub
		CmpExecSqlBO bo = cmpExecSqlMapper.get(CmpExecSqlBO.getFlowInfoId(),CmpExecSqlBO.getComponentSeq());

		return bo;
	}
	/**  
	 * @Title: getConfig
	 * @Description: TODO(描述)
	 * @param flowId
	 * @param seq
	 * @return 
	 * @see com.fsxgt.datagrid.core.service.IComponentService#getConfig(java.lang.String, java.lang.String) 
	 * @author liuyi
	 * @date 2023-05-25 15:24:47 
	 */ 
	@Override
	public Object getConfig(String flowId, String seq) {
		// TODO Auto-generated method stub
		
		CmpExecSqlBO bo = new CmpExecSqlBO();
		bo.setFlowInfoId(flowId);
		bo.setComponentSeq(seq);
		
		return getCmpExecSql(bo);
	}
	
	public void execSql(Long datasourceId, String sql, Map params, String enableMybatis, String enableTransaction)
			throws Exception {
		// TODO Auto-generated method stub
		TransactionStatus status = null;
		DataSourceTransactionManager transactionManager= null;
		
		if(enableTransaction.equals("1")) {
			transactionManager=new DataSourceTransactionManager(queryComponent.getHandler(datasourceId).getDatasource());
			TransactionDefinition def = new DefaultTransactionDefinition();
		    status = transactionManager.getTransaction(def);
		}
	    
	    try {
	    	if (enableMybatis != null && !enableMybatis.equals("0")) {
				String s = sql.trim().toUpperCase();
				log.info("执行sql:" + sql );
				if (s.indexOf("UPDATE") == 0) {
					mybatisComponent.update(datasourceId, sql, params);
				} else if (s.indexOf("DELETE") == 0) {
					mybatisComponent.delete(datasourceId, sql, params);
				} else if (s.indexOf("INSERT") == 0) {
					mybatisComponent.insert(datasourceId, sql, params);
				} else if (s.indexOf("ALTER") == 0) {
					mybatisComponent.update(datasourceId, sql, params);
				}
				
			} else {
				DbHandler dbHandler = queryComponent.getHandler(datasourceId);
				String sqls[] = sql.split(";");
				for (String s : sqls) {
					if (s.trim().length() != 0) {
						ParamBO param = ParamUtil.transform(s, params);
						if (s.toUpperCase().indexOf("SELECT") == 0) {
							;
						} else {
							log.info("执行sql:" + s );
							int c = dbHandler.update(param.getText(), param.getParams());
						}
						
					}
				}
			}
	    	if(transactionManager!=null) {
	    		transactionManager.commit(status);
	    	}
	    	
	    }catch(Exception e) {
	    	if(transactionManager!=null) {
	    		transactionManager.rollback(status);
	    	}
	    	
	    	throw e;
	    }
	      

		

	}
}
